

class rect:
	def __init__(self, x=0, y=0, w=1920, h=1080):
		self.x = x
		self.y = y
		self.w = w
		self.h = h
	def __str__(self):
		return "{x:"+str(self.x)+",y:"+str(self.y)+",w:"+str(self.w)+",h:"+str(self.h)+"}"
	def fromBytes(dataBytes):
		rect1 = rect()
		rect1.x = dataBytes[0]+(dataBytes[1]<<8)
		rect1.y = dataBytes[2]+(dataBytes[3]<<8)
		rect1.w = dataBytes[4]+(dataBytes[5]<<8)
		rect1.h = dataBytes[6]+(dataBytes[7]<<8)
		return rect1
	def toBytes(self):
		return self.x.to_bytes(2, byteorder= 'little')+\
			self.y.to_bytes(2, byteorder='little')+\
			self.w.to_bytes(2, byteorder='little')+\
			self.h.to_bytes(2, byteorder='little')

class layer:
	def __init__(self, scale=rect(), crop=rect(), input=0, freeze=0, flip=0, alpha=0, pos=0):
		self.scale = scale
		self.crop = crop
		self.input = input
		self.freeze = freeze
		self.flip = flip
		self.alpha = alpha
		self.pos = pos
	def __str__(self):
		return "{input:"+"{:2d}".format(self.input)+", scale:"+str(self.scale)+", crop:"+str(self.crop)+\
			", alpha:"+str(self.alpha)+", pos:"+str(self.pos)+"}"		
	def fromBytes(self, dataBytes):
		crop = rect.fromBytes(dataBytes)
		scale = rect.fromBytes(dataBytes[8:])
		input = dataBytes[18]
		freeze = dataBytes[16]
		flip = dataBytes[17]
		self.crop = crop
		self.scale = scale
		self.input = input
		self.freeze = freeze
		self.flip = flip
		if(len(dataBytes)>=21):
			self.alpha = dataBytes[19]
			self.pos = dataBytes[20]
		
		return self
	def toBytes(self):
		return self.crop.toBytes()+self.scale.toBytes()+\
			self.freeze.to_bytes(1, byteorder='little')+\
			self.flip.to_bytes(1, byteorder='little')+\
			self.input.to_bytes(1, byteorder='little')
	def toLayerBytes(self):
		return self.toBytes()+\
			self.alpha.to_bytes(1, byteorder='little')+\
			self.pos.to_bytes(1, byteorder='little')
class other:
	def __init__(self):
		self.currentView = 0
class view:
	def __init__(self, maxLayer=16):
		self.layer = [layer()]*maxLayer
		for i in range(0, maxLayer):
			self.layer[i] = layer(pos=maxLayer-1-i)
		self.other = other()
		self.name = bytes(16)		
	def __str__(self):
		result = "view{}:".format(self.other.currentView)+"{\n"
		for i in range(0, len(self.layer)):
			result+="layer{:2d}:".format(i)+str(self.layer[i])+"\n"
		result += "}"
		return result
	def fromBytes(self, dataBytes):
		for i in range(0, len(self.layer)):
			self.layer[i].fromBytes(dataBytes[21*i:])
		self.name = dataBytes[21*len(self.layer):21*len(self.layer)+16]
	def toBytes(self):
		dataBytes = bytes()
		for i in range(0, len(self.layer)):
			dataBytes += self.layer[i].toLayerBytes()
		dataBytes += self.name
		return dataBytes
		
	

class inputSet:
	def __init__(self, scale=rect(), crop=rect(), input=0, freeze=0, flip=0, alpha=128):
		self.scale = scale
		self.crop = crop
		self.input = input
		self.freeze = freeze
		self.flip = flip
		self.alpha = alpha
		self.layerOnOff = 0x01
		self.brightness = 50
		self.contrast = 50
		self.sharpness = 50
		self.saturation = 50
		self.colorTempRed = 50
		self.colorTempGreen = 50
		self.colorTempBlue = 50
		self.rotate = 0
		self.gainR = 50
		self.gainG = 50
		self.gainB = 50
		self.hue = 50
		
	
	def __str__(self):
		return "{input:"+str(self.input)+", scale:"+str(self.scale)+", crop:"+str(self.crop)+\
			", alpha:"+str(self.alpha)+", layerOnOff:{:02X}".format(self.layerOnOff)+"}"
	
	def fromBytes(self, dataBytes):
		self.crop = rect.fromBytes(dataBytes)
		self.scale = rect.fromBytes(dataBytes[8:])
		self.input = dataBytes[18]
		self.freeze = dataBytes[16]
		self.flip = dataBytes[17]
		self.alpha = dataBytes[19]
		self.layerOnOff = dataBytes[20]
		if(len(dataBytes)>=28):
			self.brightness = dataBytes[21]
			self.contrast = dataBytes[22]
			self.sharpness = dataBytes[23]
			self.saturation = dataBytes[24]
			self.colorTempRed = dataBytes[25]
			self.colorTempGreen = dataBytes[26]
			self.colorTempBlue = dataBytes[27]
			
		if(len(dataBytes)>=33):
			self.rotate = dataBytes[28]
			self.gainR = dataBytes[29]
			self.gainG = dataBytes[30]
			self.gainB = dataBytes[31]
			self.hue = dataBytes[32]
		return self
	def toBytes(self):
		return self.crop.toBytes()+self.scale.toBytes()+\
			self.freeze.to_bytes(1, byteorder='little')+\
			self.flip.to_bytes(1, byteorder='little')+\
			self.input.to_bytes(1, byteorder='little')+\
			self.alpha.to_bytes(1, byteorder='little')+\
			self.layerOnOff.to_bytes(1, byteorder='little')+\
			self.brightness.to_bytes(1, byteorder='little')+\
			self.contrast.to_bytes(1, byteorder='little')+\
			self.sharpness.to_bytes(1, byteorder='little')+\
			self.saturation.to_bytes(1, byteorder='little')+\
			self.colorTempRed.to_bytes(1, byteorder='little')+\
			self.colorTempGreen.to_bytes(1, byteorder='little')+\
			self.colorTempBlue.to_bytes(1, byteorder='little')+\
			self.rotate.to_bytes(1, byteorder='little')+\
			self.gainR.to_bytes(1, byteorder='little')+\
			self.gainG.to_bytes(1, byteorder='little')+\
			self.gainB.to_bytes(1, byteorder='little')+\
			self.hue.to_bytes(1, byteorder='little')
			
class inputSource:
	def __init__(self, type=0, format=0x7f, width=1920, height=1080, vfreq=60):
		self.type = type
		self.format = format
		self.width = width
		self.height = height
		self.vfreq = vfreq
	def __str__(self):
		return "{type:"+str(self.type)+", format:"+str(self.format)+", width:"+str(self.width)+\
			", height:"+str(self.height)+", vfreq:{}".format(self.vfreq)+"}"
class network:
	def __init__(self):
		self.IP_adr = [192, 168, 0, 100]
		self.DefGW = [192, 168, 0, 1]
		self.NetMask = [255, 255, 255, 0]
		self.MAC_adr = [0, 0, 0, 0, 0, 0]
		self.DHCP = 0
	def __str__(self):
		return "{IP:"+"{}.{}.{}.{}".format(self.IP_adr[0], self.IP_adr[1], self.IP_adr[2], self.IP_adr[3])+\
			", DHCP:{}".format(self.DHCP)+\
			", DefGW:{}.{}.{}.{}".format(self.DefGW[0], self.DefGW[1], self.DefGW[2], self.DefGW[3])+\
			", NetMask:{}.{}.{}.{}".format(self.NetMask[0], self.NetMask[1], self.NetMask[2], self.NetMask[3])+\
			", MAC:{:02X}-{:02X}-{:02X}-{:02X}-{:02X}-{:02X}".format(self.MAC_adr[0],self.MAC_adr[1], \
			self.MAC_adr[2], self.MAC_adr[3], self.MAC_adr[4], self.MAC_adr[5])+"}"
if __name__ == "__main__":
	rect1 = rect.fromBytes(b'\x00\x00\x00\x00\x80\x078\x04\x00')
	print("rect:", rect1)
	print("rect:", rect1.toBytes(), len(rect1.toBytes()))
	print(rect.fromBytes(rect1.toBytes()))
	layer1 = layer()
	print(layer1)
	print(layer1.toBytes())
	print(layer1.fromBytes(layer1.toBytes()))
	
	layer2 = layer()
	layer2.fromBytes(b'\x00\x00\x00\x00\x80\x078\x04\x00\x008\x04\x80\x078\x04\x00\x000\x80O\xc1')
	print(layer2)
	inputSet1 = inputSet();
	print(inputSet1)
	print("inputSet1:", inputSet1.toBytes())
	inputSet2 = inputSet()
	inputSet2.fromBytes(inputSet1.toBytes())
	print("inputSet2:", inputSet2)
	view1 = view()
	print(view1)
	#print(view1.toBytes())
	#view1.fromBytes(view1.toBytes())
	#print(view1)
	print(network())
